package com.zly.framework.rbac.model.entity;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zly.framework.rbac.vaildate.EntityInsert;
import com.zly.framework.rbac.vaildate.EntityUpdate;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;

import java.io.Serial;
import java.util.List;


@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@ExcelIgnoreUnannotated
@Schema(name = "用户信息")
@Table(name = "sys_user")
@TableName("sys_user")
@ToString(callSuper = true)
public class User extends BaseEntity {

    @Serial
    private static final long serialVersionUID = 1L;

    @ExcelProperty("用户名")
    @Column(name = "username")
    @Schema(description = "登录账号")
    @NotBlank(message = "用户名不能为空", groups = {EntityInsert.class})
    private String username;

    @ExcelProperty("昵称")
    @Column(name = "nickname")
    @Schema(description = "昵称")
    @Length(min = 1, max = 20, message = "昵称的长度为1-20", groups = {EntityInsert.class, EntityUpdate.class})
    private String nickname;

    @ExcelProperty("密码")
    @Column(name = "password")
    @Schema(description = "密码")
    @NotBlank(message = "密码不能为空", groups = {EntityInsert.class})
    private String password;

    @ExcelProperty("联系电话")
    @Column(name = "phone")
    @Schema(description = "联系电话")
    @Pattern(regexp = "^1[3-9][0-9]{9}$", message = "手机号格式有误", groups = {EntityInsert.class, EntityUpdate.class})
    private String phone;

    @ExcelProperty("邮箱")
    @Column(name = "email")
    @Schema(description = "邮箱")
    @Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", message = "邮箱格式错误", groups = {EntityInsert.class, EntityUpdate.class})
    private String email;

    @ExcelProperty("描述")
    @Column(name = "description")
    @Schema(description = "描述")
    private String description;

    @ExcelProperty("头像")
    @Column(name = "photo")
    @Schema(description = "头像")
    private String photo;

    @ExcelProperty("性别")
    @Column(name = "sex")
    @Schema(description = "性别")
    private int sex;

    @Transient
    @TableField(exist = false)
    @Schema(description = "角色")
    private List<Role> roles;

    @Transient
    @Schema(description = "权限")
    @TableField(exist = false)
    private List<Permission> permissions;
}

